// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/sparepartshospital/api/WarehouseManageApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/sparepartshospital/model/WarehouseListQueryReq'
import AddClass from '@api/sparepartshospital/model/WarehouseOperateReq'
import DelClass from '@api/sparepartshospital/model/WarehouseReq'
import BaseClass from '@api/sparepartshospital/model/WarehouseDto'

const EditClass = AddClass

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']
const BASE = [BaseClass, 'warehouseDto']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询仓库列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM)
    const resp = await CORE.grpc(TIP, CLIENT, 'queryWarehouseList', DTO)
    return [resp.comQuery.totalCount, resp.content || []]
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  transAdd(one) {
    return { ...one, warehouseCode: '' }
  },
  async add(one, TIP = '新增仓库') {
    const DTO = CORE.grpcDTO(AddClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'insertWarehouse', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '修改仓库') {
    const DTO = CORE.grpcDTO(EditClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'updateWarehouse', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除仓库') {
    const DTO = CORE.grpcDTO(DelClass, { ids: [id] })
    await CORE.grpc(TIP, CLIENT, 'deleteWarehouse', DTO)
    return CORE.grpcOK(TIP)
  },
}
